package com.success.application.shop.activity;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.reflect.TypeToken;
import com.success.application.api.YuanbaoApi;
import com.success.application.base.BaseDetailActivity;
import com.success.application.common.param.Params;
import com.success.application.common.result.Result;
import com.success.application.myapplication.R;
import com.success.application.shop.bean.SheTuan;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.lang.reflect.Type;
import java.util.Map;

import butterknife.Bind;

/**
 * Created by Administrator on 2017/12/22.
 */

public class SheTuanDetailActivity extends BaseDetailActivity<SheTuan> {
    private SheTuan mshetuan;
    public static final String BUNDLE_KEY_SHETUAN = "BUNDLE_KEY_SHETUAN";
    @Bind(R.id.iv_portrait)
    ImageView logo;//社团图片
    @Bind(R.id.tv_nick)
    TextView name;//社团主题
    @Bind(R.id.tv_jianjie)
    TextView shijian;//发布时间
//    @Bind(R.id.tv_dizhi)
//    TextView dizhi;//发布人
//    @Bind(R.id.tv_beizhu)
//    TextView beizhu;//备注
    @Bind(R.id.aixin_content)
    WebView content;//详情
//    @Bind(R.id.tv_renshu)
//    TextView renshu;//报名人数
    @Override
    protected Type getType() {
        return new TypeToken<Result<SheTuan>>() {
        }.getType();
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.shop_shetuan_detail;
    }
    @Override
    protected int getTitleRes() {
        return R.string.shetuan_details;
    }
    //从列表传参
    @Override
    protected boolean initBundle(Bundle bundle) {
        super.initBundle(bundle);
        mshetuan = (SheTuan) getIntent().getSerializableExtra(BUNDLE_KEY_SHETUAN);
        if (mshetuan == null) {
            Toast.makeText(this, "对象没找到", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
    //
    @Override
    protected void initWidget() {
        super.initWidget();
        //实例化活动报名会员的列表
    }

    @Override
    protected void initData() {
        super.initData();
        Params p = new Params();
        p.put("id",mshetuan.getId());
        try {
            YuanbaoApi.getNoticeDetails(p, mHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getNewContent(String htmltext){

        Document doc= Jsoup.parse(htmltext);
        Elements elements=doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width","100%").attr("height","auto");
        }

        Log.d("VACK", doc.toString());
        return doc.toString();
    }

    @Override
    protected void setData(Result<SheTuan> result) {
        Map mMap = result.getData();
        if(mMap.get("xxgg_bt")!=null){
            name.setText(mMap.get("xxgg_bt").toString());
        }
        if(mMap.get("xxgg_bttp")!=null){
            Glide.with(getApplication()).load(mMap.get("xxgg_bttp").toString()).into(logo);
        }
        if(mMap.get("xxgg_fbsj")!=null){
            shijian.setText(mMap.get("xxgg_fbsj").toString());
        }
//        if(mMap.get("xxgg_fbr")!=null){
//            dizhi.setText(mMap.get("xxgg_fbr").toString());
//        }
//        if(mMap.get("xxgg_bz")!=null){
//            beizhu.setText(mMap.get("xxgg_bz").toString());
//        }
        if(mMap.get("xxgg_nr")!=null){
            //  加载、并显示HTML代码
            content.loadDataWithBaseURL("http://47.92.92.215:8087", getNewContent(mMap.get("xxgg_nr").toString()), "text/html" , "utf-8", null);
            content.getSettings().setJavaScriptEnabled(true);
            content.getSettings().setDomStorageEnabled(true);
            content.getSettings().setJavaScriptEnabled(true);
            content.getSettings().setUseWideViewPort(true);
            content.getSettings().setBlockNetworkImage(false);
            content.getSettings().setLoadWithOverviewMode(true);
            if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP){
                content.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            }
            content.setWebChromeClient(new WebChromeClient());
            Log.d("","");
        }
    }
}
